const { mysql } = require('../qcloud')

module.exports = async (ctx) => {
    const { id } = ctx.request.query
    const bookinfo = await mysql('books')
                        .select('books.*', 'cSessionInfo.user_info')
                        .join('cSessionInfo', 'books.openid', 'cSessionInfo.open_id')
                        .where('id',id)
                        .first()
    await mysql('books').where('id', id)
        .increment('count', 1)
    const info = JSON.parse(bookinfo.user_info)
    ctx.state.data = Object.assign({},bookinfo,{
        tags:bookinfo.tags.split(','),
        summary:bookinfo.summary.split('\n'),
        user_info:{
            nickname: info.nickName,
            avatarUrl: info.avatarUrl
        }
    })
}